package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.controller.request.BorrowPageRequest;
import com.example.springboot.entity.Borrow;
import com.example.springboot.service.impl.BorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/borrow")
public class BorrowController {


    @Autowired
    BorrowService borrowService;

    //保存
    @PostMapping("/save")
    public Result login(@RequestBody Borrow borrow) {
        borrowService.save(borrow);
        return Result.success();
    }

    //更新
    @PutMapping("/update")
    public Result update(@RequestBody Borrow borrow) {
        borrowService.update(borrow);
        return Result.success();
    }


    //获取
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Borrow admins = borrowService.getById(id);
        return Result.success(admins);
    }

    //单个删除
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        borrowService.deleteById(id);
        return Result.success();
    }


    //   查询全部
    @GetMapping("/list")
    public Result list() {
        List<Borrow> borrows = borrowService.list();
        return Result.success(borrows);
    }


    //    分页查询
    @GetMapping("/page")
    public Result page(BorrowPageRequest borrowPageRequest) {
        return Result.success(borrowService.page(borrowPageRequest));
    }


    @GetMapping("/lineCharts/{timeRange}")
    public Result lineCharts(@PathVariable String timeRange) {
        return Result.success(borrowService.getCountByTimeRange(timeRange));
    }
}
